'use strict';

const Controller = require('egg').Controller;

class ReviewController extends Controller {
  // 创建评价
  async createReview() {
    const { ctx } = this;
    const reviewData = ctx.request.body;
    
    console.log('\n========== 收到创建评价请求 ==========');
    console.log('请求时间:', new Date().toISOString());
    console.log('请求数据:', JSON.stringify(reviewData, null, 2));
    console.log('请求来源:', ctx.request.headers['user-agent']);
    console.log('========================================\n');
    
    try {
      const result = await ctx.service.job1.review.createReview(reviewData);
      
      console.log('\n========== 创建评价成功 ==========');
      console.log('返回数据:', JSON.stringify(result, null, 2));
      console.log('====================================\n');
      
      ctx.body = {
        code: 0,
        data: result,
        msg: '评价创建成功'
      };
    } catch (error) {
      console.error('\n========== 创建评价失败 ==========');
      console.error('错误信息:', error.message);
      console.error('错误堆栈:', error.stack);
      console.error('====================================\n');
      
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '创建评价失败'
      };
    }
  }

  // 获取企业评价列表
  async getEnterpriseReviews() {
    const { ctx } = this;
    const { enterpriseId } = ctx.params;
    const { page = 1, pageSize = 10 } = ctx.query;
    
    console.log('\n========== 获取企业评价列表 ==========');
    console.log('企业ID:', enterpriseId);
    console.log('分页参数:', { page, pageSize });
    console.log('参数类型:', { page: typeof page, pageSize: typeof pageSize });
    
    try {
      const result = await ctx.service.job1.review.getEnterpriseReviews(enterpriseId, { page, pageSize });
      
      console.log('查询成功，返回数据:', {
        total: result.total,
        listCount: result.list ? result.list.length : 0
      });
      console.log('====================================\n');
      
      ctx.body = {
        code: 0,
        data: result,
        msg: '获取成功'
      };
    } catch (error) {
      console.error('\n========== 获取评价列表失败 ==========');
      console.error('错误信息:', error.message);
      console.error('错误堆栈:', error.stack);
      console.error('====================================\n');
      
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '获取评价列表失败'
      };
    }
  }

  // 删除评价
  async deleteReview() {
    const { ctx } = this;
    const { reviewId } = ctx.params;
    
    try {
      const result = await ctx.service.job1.review.deleteReview(reviewId);
      ctx.body = {
        code: 0,
        data: result,
        msg: '删除成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '删除失败'
      };
    }
  }

  // 回复评价
  async replyReview() {
    const { ctx } = this;
    const { reviewId } = ctx.params;
    const { replyContent } = ctx.request.body;
    
    try {
      const result = await ctx.service.job1.review.replyReview(reviewId, replyContent);
      ctx.body = {
        code: 0,
        data: result,
        msg: '回复成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '回复失败'
      };
    }
  }

  // 举报评价
  async reportReview() {
    const { ctx } = this;
    const { reviewId } = ctx.params;
    const { reportReason } = ctx.request.body;
    
    try {
      const result = await ctx.service.job1.review.reportReview(reviewId, reportReason);
      ctx.body = {
        code: 0,
        data: result,
        msg: '举报成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '举报失败'
      };
    }
  }

  // 处理投诉
  async processComplaint() {
    const { ctx } = this;
    const { complaintId } = ctx.params;
    const processData = ctx.request.body;
    
    try {
      const result = await ctx.service.job1.review.processComplaint(complaintId, processData);
      ctx.body = {
        code: 0,
        data: result,
        msg: '处理成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '处理投诉失败'
      };
    }
  }

  // 获取投诉列表
  async getComplaints() {
    const { ctx } = this;
    const { page = 1, pageSize = 10, status } = ctx.query;
    
    try {
      const result = await ctx.service.job1.review.getComplaints({ page, pageSize, status });
      ctx.body = {
        code: 0,
        data: result,
        msg: '获取成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '获取投诉列表失败'
      };
    }
  }
}

module.exports = ReviewController;














